package com.zjweu.waterlevelprediction.controller;

import com.zjweu.waterlevelprediction.pojo.PreData;
import com.zjweu.waterlevelprediction.service.PredictionWaterLevels;
import de.siegmar.fastcsv.reader.CsvContainer;
import de.siegmar.fastcsv.reader.CsvReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@Controller
public class Prediction {

    @Autowired
    private PredictionWaterLevels preWaterLevels;

    @RequestMapping("/preWater")
    @ResponseBody
    public Object preWater1(@RequestBody Map<String,String> map) throws IOException, ParseException, InterruptedException {

        //原始数据
        File file = new File("src/main/resources/csv/fallraw_7041JA26clea.csv");
        String csvPath = file.getAbsolutePath();

        CsvReader csvReader = new CsvReader();
        csvReader.setContainsHeader(true);
        List<PreData> list = new ArrayList<>();
        CsvContainer csv = csvReader.read(file, StandardCharsets.UTF_8);
        for (int i = csv.getRowCount() - 48; i < csv.getRowCount()-24; i++) {
            PreData preData = new PreData();
            preData.setData(csv.getRows().get(i).getField(12));
            preData.setTime(csv.getRows().get(i).getField(0));
            list.add(preData);
        }
        /*//        for (CsvRow row : csv.getRows()) {
        //通过头获取对应属性
//            index,Unnamed: 0,times,temperature,humidity,winddirection,windpower,rains,rains7041JA23,waterlevels7041JA23,rains7041JA24,waterlevels7041JA24,waterlevels

//        }*/
        String mx = map.get("mx");
        if ("".equals(mx)){
            for (int i = csv.getRowCount() - 24; i < csv.getRowCount(); i++) {
                PreData preData = new PreData();
                preData.setData(csv.getRows().get(i).getField(12));
                preData.setTime(csv.getRows().get(i).getField(0));
                list.add(preData);
            }
            return list;
        }
        //预测数据
        List<Double> preList = preWaterLevels.preWaterLevels(mx, csvPath);

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(df.parse("2022-05-17 09:00:00"));

        for (int i = 0; i < preList.size(); i++) {
            PreData d = new PreData();
            d.setData(preList.get(i).toString());
            c.add(Calendar.HOUR_OF_DAY, 1);
            d.setTime(df.format(c.getTime()));
            list.add(d);
        }
        return list;
    }
}
